<template>
	<view>

		<view class="box" v-for="(item,index) in list" :key="index">
			<view class="item">
				<view class="left">
					<view class="title"><text>订单号:</text>{{ item.orderno }}</view>
					<view class="title"><text>下单时间:</text>{{ doreset(item.createtime) }}</view>
					<view class="title"><text>订单类型:</text>{{ item.typeName }}</view>
				</view>
				<view class="right">
					<text>{{ item.money }}元</text>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		mapState,
		mapActions
	} from 'vuex'
	export default {
		computed: {
			...mapState(['token'])
		},
		data() {
			return {
				page: 1,
				pages: null,
				list: []
			};
		},
		//下拉刷新
		onPullDownRefresh() {
			console.log('下拉刷新');
			setTimeout(() => {
				uni.stopPullDownRefresh();
				this.page = 1;
				this.list = [];
				this.getData();
			}, 1000)
		},
		//上拉加载
		onReachBottom() {
			console.log('触发上拉加载');
			if (this.page < this.pages) {
				this.page++;
				this.getData();
			} else {
				uni.showToast({
					title: '到底了!',
					icon: 'none'
				})
			}
		},
		onLoad() {
			//获取数据
			this.getData();
		},
		methods: {
			//获取数据
			async getData() {
				try {
					let params = {
						token: this.token,
						page: this.page
					};
					let res = await this.$request.request.get('/api/user/order', params)
					console.log(res);
					if (res.code == 200) {
						this.pages = res.data.pages;
						this.list = this.list.concat(res.data.list);
					}
				} catch (err) {
					this.$request.error(err, this)
				}
			},
			//转换时间戳
			doreset(e) {
				//转换时间戳
				if ((e + '').length != 13) {
					var date = new Date(e * 1000); //如果date为13位不需要乘1000
					var Y = date.getFullYear() + '-';
					var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
					var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
					var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
					var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
					var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
				} else {
					var date = new Date(e); //如果date为13位不需要乘1000
					var Y = date.getFullYear() + '-';
					var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
					var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
					var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
					var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
					var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
				}
				return Y + M + D + h + m + s;
			}
		}
	}
</script>

<style lang="scss">
	@import 'myOrder.scss'
</style>
